<template>
	<view>
		<!-- 地址 -->
		<view class="address-section">
			<view class="order-content" @click="handlerAddress">
				<view style="margin-right: 20rpx;">
					<u-icon name="map" color="#909193" size="32"></u-icon>
				</view>
				<view class="cen" v-if="addressData.default">
					<view class="top">
						<text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.mobile}}</text>
					</view>
					<text class="address">{{addressData.address}} {{addressData.area}}</text>
				</view>
				<view class="cen" v-else>
					<view class="top">
						<view class="name">请选择收获地址</view>
					</view>
				</view>
				<u-icon name="arrow-right" color="#909193" size="32"></u-icon>
			</view>

			<image class="a-bg"
				src="">
			</image>
		</view>

		<view class="goods-section">
			<!-- 商品列表 -->
			<view class="g-item" v-for="(item, index) of goodsCard" :key="index">
				<image :src="item.sku_image" mode="aspectFill"></image>
				<view class="right">
					<text class="title">{{ item.goods_name }}</text>
					<view class="spec"> {{ item.introduction }} </view>
					<view class="spec" v-for="(son, index) in item.specsData"> {{ son }} </view>
					<view class="price-box">
						<text class="price">&yen;{{ item.price }}</text>
						<text class="number">x {{ item.currentNum }}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 金额明细 -->

		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit">商品总金额</text>
				<text class="cell-tip">&yen;{{ cartAllPrice }}</text>
			</view>
			<!-- 	<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">优惠金额</text>
				<text class="cell-tip red">-￥35</text>
			</view> -->
			<view class="yt-list-cell b-b">
				<text class="cell-tit">运费</text>
				<text class="cell-tip">免运费</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit">备注</text>
				<input class="desc" type="text" v-model="desc" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">&yen;</text>
				<text class="price">{{ cartAllPrice }}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>

	</view>
</template>

<script>
	import request from "@/api/request";
	export default {
		data() {
			return {
				desc: '', //备注
				goodsCard: [],
				payDto: {
					token: '',
					openid: ''
				},
				addressData: {
					name: '',
					mobile: '',
					address: '',
					area: '',
					default: false,
				}
			}
		},
		onLoad({
			flag
		}) {
			if (flag) {
				this.goodsCard = uni.getStorageSync('buy')
			} else {
				this.goodsCard = uni.getStorageSync('card')
			}
			this.payDto.token = uni.getStorageSync('token')
			this.getOpenId()

		},
		computed: {
			cartAllPrice() { //计算购物车总数
				return this.goodsCard.reduce((acc, cur) => acc + cur.currentNum * cur.price, 0)
			},
		},
		methods: {
			getOpenId() {
				const that = this
				uni.login({
					provider: 'weixin', //使用微信登录
					success: (loginRes) => {
						request({
							url: '/kx/index/getOpenid',
							method: 'POST',
							data: {
								code: loginRes.code
							},
							successCb: (res) => {
								that.payDto.openid = res.data || ''
							},
							failCb: (err) => {
								uni.$u.toast('未知错误请联系客服')
							}
						})
					}
				});
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type) {
				this.payType = type;
			},
			submit() {
				const goodsArr = []
				if (!this.addressData.name) {
					uni.$u.toast('请选择收获地址')
					return
				}
				uni.showLoading({
					title: '加载中...'
				})
				console.log(this.goodsCard)
				this.goodsCard.forEach(item => {
					if (item.goods_spec_format) {
						const strTemp = item.specsData.join(',')
						console.log(item.specsData)
						goodsArr.push({
							sku_id: item.sku_id,
							sku_name: item.specsData.join(',') + ',' + item.goods_name,
							numvalue: item.currentNum
						})
					} else {
						goodsArr.push({
							sku_id: item.sku_id,
							sku_name: item.goods_name,
							numvalue: item.currentNum
						})
					}
				})
				const memberArr = {
					name: this.addressData.name,
					mobile: this.addressData.mobile,
					address: this.addressData.address + this.addressData.area,
				}
				this.payDto.goodsArr = {
					goodsArr,
					memberArr,
					remark: this.desc
				}
				request({
					url: '/kx/ShopOrder/createOrder',
					method: "POST",
					data: this.payDto,
					successCb: ({ data }) => {
						uni.hideLoading()
					   uni.requestPayment({
					      provider: "wxpay",
						   appid: data.credential.appId,  // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
						   nonceStr: data.credential.nonceStr, // 随机字符串
						   package: data.credential.package,        // 固定值
						   prepayId: data.credential.prepayId, // 统一下单订单号 
						   timeStamp: data.credential.timeStamp,        // 时间戳（单位：秒）
						   signType: "RSA", // 签名，这里用的 MD5/RSA 签名
						   paySign:data.credential.paySign,
					       success(res) {
							uni.$u.toast('支付成功')   
						   },
					       fail(e) {
							 uni.$u.toast('取消支付') 

						   }
					   })	
					},
					failCb: () => {
							uni.hideLoading()
					}

				})
			},
			handlerAddress() {
				const that = this
				uni.chooseAddress({
					success({
						provinceName,
						cityName,
						countyName,
						detailInfo,
						telNumber,
						userName
					}) {
						that.addressData.name = userName
						that.addressData.mobile = '15959108888'
						that.addressData.address = provinceName + cityName + countyName
						that.addressData.area = detailInfo
						that.addressData.default = true
						console.log()
					},
					fail(err) {

					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f6f6f6;
		padding-bottom: 100rpx;
	}

	.address-section {
		padding: 20rpx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
			padding: 0 20rpx;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90rpx;
			color: #888;
			font-size: 44rpx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28rpx;
			color: dark;
		}

		.name {
			font-size: 30rpx;
			margin-right: 24rpx;
		}

		.address {
			margin-top: 16rpx;
			margin-right: 20rpx;
			color: #909399;
			font-size: 28rpx;
		}

		.icon-you {
			font-size: 32rpx;
			margin-right: 30rpx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5rpx;
		}
	}

	.goods-section {
		margin-top: 16rpx;
		background: #fff;
		padding: 20rpx 0;
		max-height: 480rpx;
		overflow-y: auto;

		.g-item {
			display: flex;
			margin: 20rpx 30rpx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140rpx;
				height: 140rpx;
				border-radius: 4rpx;
			}

			.right {
				flex: 1;
				padding-left: 24rpx;
				overflow: hidden;
			}

			.title {
				font-size: 30rpx;
				color: #303133;
			}

			.spec {
				font-size: 26rpx;
				color: #909399;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32rpx;
				padding-top: 10rpx;

				.price {
					margin-bottom: 4rpx;
				}

				.number {
					font-size: 26rpx;
					margin-left: 20rpx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16rpx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10rpx 30rpx 10rpx 40rpx;
		line-height: 70rpx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30rpx;
		}

		.cell-icon {
			height: 32rpx;
			width: 32rpx;
			font-size: 22rpx;
			color: #fff;
			text-align: center;
			line-height: 32rpx;
			background: #f85e52;
			border-radius: 4rpx;
			margin-right: 12rpx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24rpx;
			margin-left: 8rpx;
			margin-right: -10rpx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26rpx;
			margin-right: 10rpx;
		}

		.cell-tip {
			font-size: 26rpx;

			&.disabled {}

			&.active {}

			&.red {}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90rpx;
			}
		}

		.desc {
			flex: 1;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40rpx;
		margin-top: 16rpx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20rpx;
			line-height: 1;
			height: 110rpx;
			position: relative;
		}

		.icon-weixinzhifu {
			width: 80rpx;
			font-size: 40rpx;
			color: #6BCC03;
		}

		.icon-alipay {
			width: 80rpx;
			font-size: 40rpx;
			color: #06B4FD;
		}

		.icon-xuanzhong2 {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60rpx;
			height: 60rpx;
			font-size: 40rpx;
		}

		.tit {
			font-size: 32rpx;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		width: 100%;
		height: 120rpx;
		justify-content: space-between;
		font-size: 30rpx;
		background-color: #fff;
		z-index: 998;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);

		.price-content {
			padding-left: 30rpx;
			color: #606266;
		}

		.price-tip {
			margin-left: 8rpx;
			color: #fa436a;
		}

		.price {
			font-size: 36rpx;
			color: #fa436a;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280rpx;
			height: 100%;
			color: #fff;
			font-size: 32rpx;
			background-color: #DBA871;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item {
		display: flex;
		flex-direction: column;
		margin: 20rpx 24rpx;
		background: #fff;

		.con {
			display: flex;
			align-items: center;
			position: relative;
			height: 120rpx;
			padding: 0 30rpx;

			&:after {
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}

		.left {
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100rpx;
		}

		.title {
			font-size: 32rpx;
			margin-bottom: 10rpx;
		}

		.time {
			font-size: 24rpx;
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26rpx;
			height: 100rpx;
		}

		.price {
			font-size: 44rpx;

			&:before {
				content: '￥';
				font-size: 34rpx;
			}
		}

		.tips {
			font-size: 24rpx;
			line-height: 60rpx;
			padding-left: 30rpx;
		}

		.circle {
			position: absolute;
			left: -6rpx;
			bottom: -10rpx;
			z-index: 10;
			width: 20rpx;
			height: 20rpx;
			background: #f3f3f3;
			border-radius: 100px;

			&.r {
				left: auto;
				right: -6rpx;
			}
		}
	}
</style>